package com.fitso.model.bean.journal;

import java.io.Serializable;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
import org.joda.time.DateTimeFieldType;

import com.fitso.model.bean.ModelBean;
import com.fitso.model.bean.User;
import com.fitso.model.bean.nutrition.Food;

public class FoodJournalEntry extends ModelBean implements Comparable<FoodJournalEntry>, Serializable {
	// Only change on breaking changes, i.e., removing fields
	private static final long serialVersionUID = 1L;

	private DateTime _at;

	private Food _food;

	private User _user;

	public FoodJournalEntry() {}

	public FoodJournalEntry(User user, DateTime at, Food food) {
		_user = user;
		_at = at;
		_food = food;
	}

	public int compareTo(FoodJournalEntry other) {
		return new CompareToBuilder().append(_at, other._at).append(_food, other._food).toComparison();
	}

	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		if (o == this) {
			return true;
		}
		if (o.getClass() != getClass()) {
			return false;
		}

		FoodJournalEntry other = (FoodJournalEntry) o;
		return new EqualsBuilder().append(_user, other._user).append(_food, other._food).isEquals()
				&& DateTimeComparator.getInstance(DateTimeFieldType.minuteOfDay()).compare(_at, other._at) == 0;
	}

	public DateTime getAt() {
		return _at;
	}

	public Food getFood() {
		return _food;
	}

	public User getUser() {
		return _user;
	}

	public int hashCode() {
		return new HashCodeBuilder().append(_user).append(_at).append(_food).toHashCode();
	}

	public void setAt(DateTime at) {
		_at = at;
	}

	public void setFood(Food food) {
		_food = food;
	}

	public void setUser(User user) {
		_user = user;
	}

	public String toString() {
		return new ReflectionToStringBuilder(this).toString();
	}
}
